package cn.cloudmenu.android;

import java.net.URI;

import org.json.JSONObject;

import android.webkit.WebView;

import cn.cloudmenu.android.activity.CoursesOrdered;

import com.strumsoft.websocket.phonegap.WebSocket;

public class CoursesOrderedWebSocket extends WebSocket {
	private MenuCoverBean menu;
	private CoursesOrdered coursesOrderedActivity;

	public CoursesOrderedWebSocket(WebView appView, URI uri, UserProfile user, MenuCoverBean menu, CoursesOrdered coursesOrderedActivity) {
		super(appView, uri, Draft.DRAFT76, user.getNickname());
		this.menu = menu;
		this.coursesOrderedActivity = coursesOrderedActivity;
	}

	public void onMessage(final String msg) {
		super.onMessage(msg);
		try {
			JSONObject json = new JSONObject(msg);
			String command = json.getString("command");
			System.out.println("---0000000000))))))))))))))))))))))))))))))CoursesOrderedWebSocket.onMessage()  msg = " + msg);
			System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||CoursesOrderedWebSocket.onMessage()");
			if ("message".equals(command)) {
				String message = json.getString("message");
				message = java.net.URLDecoder.decode(message, "utf-8");
				coursesOrderedActivity.setMessage(message);
			} else if("orderList".equals(command)){
				if(json.has("courses")){
					String courses = json.getString("courses");
					System.out.println("---0000000000))))))))))))))))))))))))))))))CoursesOrderedWebSocket.onMessage()  courses = " + courses);
					coursesOrderedActivity.setOrderedCoursesList(courses);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onOpen() {
		super.onOpen();
		try {//对于CoursesOrdered Activity, websocket open 时,应先向服务器段请求数据,
			JSONObject command = new JSONObject();
			command.put("command", "join");
			command.put("menuId", menu.getId());
			command.put("menuName", menu.getName());
			String tableName = coursesOrderedActivity.getIntent().getStringExtra("tableName");
			String opener = coursesOrderedActivity.getIntent().getStringExtra("opener");
			command.put("tableName", tableName);
			command.put("opener", opener);
			System.out.println("command =" + command + " menuId=" + menu.getId() + " tableName=" + tableName + " opener=" + opener);

			this.send(command.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void removeCourse(MenuCourseOrderedBean orderedCourse) {
		try {// 不管是“新开桌”还是“加入桌”，到了Courses Activity 都以加入方式，获取当前桌子状态即可。
			JSONObject command = new JSONObject();
			command.put("command", "removeCourse");
			command.put("orderedCourseId", orderedCourse.getOrderedId());
			this.send(command.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void orderedCoursesList(){
		try {//获取已点菜肴列表
			JSONObject command = new JSONObject();
			command.put("command", "orderList");
			System.out.println("CoursesOrderedWebSocket.orderedCoursesList() command =" + command);
			this.send(command.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void onClose() {
		super.onClose();
	}

	public void onError(final Throwable t) {
		super.onError(t);
	}

}
